package graphics.Skybox;

import graphics.GraphicalObject;

import java.io.File;

import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

public class SkyBox extends GraphicalObject {

	Texture mtexture;

	double radius = 1;
	private String textureFile;

	@Override
	public void setParameter(String name, String args[])
	{
		if (name.equals("file")) {
			textureFile = args[0];	
		}

		
		if (name.equals("radius")) {
			this.setRadius(Double.parseDouble(args[0])); 	
		}
		super.setParameter(name, args);
	}

	@Override
	public void initialize(GL gl) {
		super.initialize(gl);
			try {
				File file;
				file = new File(textureFile); 
				mtexture = TextureIO.newTexture(file, true);
			} catch (Exception e) {
				System.out.println("Error failed to read texture");
				e.printStackTrace();
			}
}



	@Override
	public void prerender(GL gl) {

		gl.glPushAttrib(GL.GL_ENABLE_BIT);
		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glDisable(GL.GL_DEPTH_TEST);
		gl.glDisable(GL.GL_LIGHTING);
		gl.glDisable(GL.GL_BLEND);

		gl.glColor4f(1,1,1,1);
		mtexture.enable();
		mtexture.bind();
		gl.glBegin(GL.GL_QUADS);

		float xl = 0;
		float xr = 0.125f;
		// front
		gl.glTexCoord2f(xl, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
		gl.glTexCoord2f(xl, 1); gl.glVertex3f(  0.5f,  0.5f, -0.5f );

		xl = xr;
		xr += 0.125f;
		// back
		gl.glTexCoord2f(xl, 0); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f(  0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f(  0.5f,  0.5f,  0.5f );
		gl.glTexCoord2f(xl, 1); gl.glVertex3f( -0.5f,  0.5f,  0.5f );

		xl = xr;
		xr += 0.125f;
		// left
		gl.glTexCoord2f(xl, 0); gl.glVertex3f(  0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f(  0.5f,  0.5f, -0.5f );
		gl.glTexCoord2f(xl, 1); gl.glVertex3f(  0.5f,  0.5f,  0.5f );


		xl = xr;
		xr += 0.125f;
		// right
		gl.glTexCoord2f(xl, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f( -0.5f,  0.5f,  0.5f );
		gl.glTexCoord2f(xl, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
		
		xl = xr;
		xr += 0.125f;
		// down
		gl.glTexCoord2f(xl, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
		gl.glTexCoord2f(xl, 1); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f(  0.5f, -0.5f,  0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
		
		xl = xr;
		xr += 0.125f;
		// up
		gl.glTexCoord2f(xl, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
		gl.glTexCoord2f(xl, 0); gl.glVertex3f( -0.5f,  0.5f,  0.5f );
		gl.glTexCoord2f(xr, 0); gl.glVertex3f(  0.5f,  0.5f,  0.5f );
		gl.glTexCoord2f(xr, 1); gl.glVertex3f(  0.5f,  0.5f, -0.5f );

		
		gl.glEnd();
		
		// Restore enable bits and matrix
		gl.glPopAttrib();
		gl.glPopMatrix();
		super.render(gl);
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}



}
